{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eea3ccb8-3da1-48af-9854-1e5865cc70d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import urllib.parse as parse\n",
    "from urllib.parse import quote, quote_plus # 将string转为url字符\n",
    "import re\n",
    "import json\n",
    "import pickle\n",
    "headers = {\"Cookie\" : \"sensorsdata2015jssdkchannel=%7B%22prop%22%3A%7B%22_sa_channel_landing_url%22%3A%22%22%7D%7D; user_locale=zh-CN; oschina_new_user=false; tz=Asia%2FShanghai; Hm_lvt_24f17767262929947cc3631f99bfd274=1722926402,1722992932,1724064045; HMACCOUNT=6B63E7258241EAF5; remote_way=http; yp_riddler_id=beb85d16-f21d-4370-aea9-2cc6d74de714; project_pr_sort=closed_at+desc; visit-gitee--2024-08-27=1; visit-gitee--2024-08-28=1; BEC=1f1759df3ccd099821dcf0da6feb0357; Hm_lpvt_24f17767262929947cc3631f99bfd274=1725264031; user_return_to_0=%2F; gitee_user=true; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%229763246%22%2C%22first_id%22%3A%22191b1c58ad41cf-0510fa97903052-4c657b58-2073600-191b1c58ad515eb%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fci.openharmony.cn%2F%22%2C%22%24latest_utm_source%22%3A%22baidu%22%2C%22%24latest_utm_medium%22%3A%22sem%22%2C%22%24latest_utm_campaign%22%3A%22enterprise%22%2C%22%24latest_utm_content%22%3A%22competition%22%2C%22%24latest_utm_term%22%3A%22git%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkxMjY2YTgzNGExZjQtMDY1NDQ3YTM0YWNjNjEtN2Y1ZDU0N2UtMjA3MzYwMC0xOTEyNjZhODM0YmY3MSIsIiRpZGVudGl0eV9sb2dpbl9pZCI6Ijk3NjMyNDYifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%229763246%22%7D%2C%22%24device_id%22%3A%22191266a834a1f4-065447a34acc61-7f5d547e-2073600-191266a834bf71%22%7D; csrf_token=4XGFi%2F94xEqQnT%2Fay%2BweeP%2Bpp7tDf0ER77EGVMG0zukuqH%2FLmJKpD6cP7O2eSY6Y564dMLqEs6Btwjz6NVTtig%3D%3D; gitee-session-n=cW9Vb2JGZENQMHBsSFB4bFBCdVZ1QnZ2cEEyOXUyQUkxRzZyTy82UW9jTStjUldBN3h3SzVUMTNFVHZSY2lMNWNLTkx0QlpKby9oNEtRZTVCWUVvd0txdkcwcUNmWFBkLzc4SENvYUFzOEFXeXExUXN1OGFvUTJBZVVFSVVKcG16a2NlWFJHSDMyZUpqVkppQnhzMzg1TUszOGYzV2xIa0t4aWd5Tm1uMzdxNDArN1BMQklMY2gyL1NpOHFOY1hHYmxlYVdlL2N5L01MbWxLZzB3ZFVHMVk4cmQrazM5UGx3amZKVE92WUZZSjh1akpYTytvMDVrTm92eFVITFJsVUx4YTlYL3ptSk8wZEZVanYyV2Y4QURjenFRZTBIQVVTV09IeEtyV2Q5bkp0WVpBOFlIKzdnN3Q0WmdQN2RJZXpJZ0J0YlpjT1pjMU1pNEg0Z0E0YXZRR1oxa1dCZ2xhZW1RZG9qaXMxaExLYUZBNFJSMFF2djI0MzVRNGhOZ0p6SWd1OTE3YTA4bWRDeTNFYXhCK1pYL2lHcDYyODcxYXVGSlNFbmRXU0dPM2xha2NHTGEwZXhZTTByWXp0TVBsTHg3eGlxK0xiekIyMm40TWRreS9uWGxSdmhmNnJ4ZWU2ZVhpbjZQTDlVRUlrQnhrMzZCNHBTMER4RWlTYXl5VEx3VXE5NXdDTzlMYTRBMzUyQnl6R1E0NUZIQVcwek16RUJxV083R3VjL0tHVitIVWZRMENTUGlKSys1RU9qbm1VelBpVjlrLzdrVEhZOHB3TndZNTdrbnR2eno1V3JSUmkyVEtaSnZkaXJXNEV1SExza1RaSUd1dmdjSUJaWlJHNC0tc2JvTGtIcXpqTkhZamFUb0NkdzFZUT09--af89a1fd83754043e0b1f7776350591bc5ff6700\"}\n",
    "proxies = {\n",
    "    'http': 'http://127.0.0.1:7890',\n",
    "    'https': 'http://127.0.0.1:7890',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d160645-25fd-4b2b-bd18-d7575cc0a683",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get(url, isText = True):\n",
    "    if (isText):\n",
    "        return requests.get(url, headers=headers, proxies=proxies).text\n",
    "    else:\n",
    "        return requests.get(url, headers=headers, proxies=proxies)\n",
    "def getSoup(url):\n",
    "    return BeautifulSoup(get(url))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16d7e57b-5185-4dd8-bd03-a6420aba76fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "gitee_link = \"https://gitee.com/openharmony/kernel_linux_5.10/issues\"\n",
    "resp = get(gitee_link)\n",
    "soup = BeautifulSoup(resp)\n",
    "urls = soup.find_all(\"a\", class_=\"title\")\n",
    "urls = [[url[\"title\"] , \"https://gitee.com/\"+url[\"href\"]] for url in urls]\n",
    "urls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "014ac8db-832b-492f-8949-b0a9dfa0eb3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getIssueState(soup):\n",
    "    return soup.find(id=\"issue-state-label\").get_text(strip=True)\n",
    "def getIssueNo(soup):\n",
    "    return soup.find(id=\"issue-number-button\").get_text(strip=True)\n",
    "def getIssueTargetId(soup):\n",
    "    return soup.find(id=\"target_id\")['value']\n",
    "def getCVE(soup):\n",
    "    s = set([tag for tag in soup.find_all(\"div\", class_=\"git-issue-description markdown-body\")])\n",
    "    assert len(s) == 1\n",
    "    tag = s.pop()\n",
    "    ret = str(tag.p.text)\n",
    "    if \"CVE-\" not in ret : \n",
    "        return None\n",
    "    return ret\n",
    "class Issue:\n",
    "    def __init__(self, title, url):\n",
    "        soup = getSoup(url)\n",
    "        self.no = getIssueNo(soup)\n",
    "        self.title = title\n",
    "        self.url = url\n",
    "        self.state = getIssueState(soup)\n",
    "        self.cve = getCVE(soup)\n",
    "        self.targetType = \"issue\"\n",
    "        self.targetId = \"\"\n",
    "        self.commit = \"评论\"\n",
    "    def __str__(self):\n",
    "         return \"Issue('no':{}, 'title':{}, 'url':{}, 'state':{})\".format(self.no, self.title, self.url, self.state)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61ce8201-8159-487e-9a3b-d1a59d9fb6c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "issue_list = [Issue(i, j) for i,j in urls]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bded819-fb6b-4758-9250-c1472b756c34",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "[[issue.no, issue.title, issue.state, issue.url]for issue in issue_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42bcff30-79fc-4a4c-b9f1-9ada801d5068",
   "metadata": {},
   "outputs": [],
   "source": [
    "[str(issue) for issue in issue_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61a0d1c2-c09f-48b3-bcb3-6b26d65c56a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "aMap = dict()\n",
    "aMap = {issue.no: issue for issue in issue_list}\n",
    "aMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a01227ca-ad39-411b-a177-8c0bc4630111",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'待办的': 877565,\n",
       " '已确认': 877569,\n",
       " '技术评审中': 1244182,\n",
       " '修复中': 877573,\n",
       " '验收中': 877572,\n",
       " '已完成': 877567,\n",
       " '已取消': 877574,\n",
       " '已拒绝': 877568,\n",
       " '挂起': 1244183}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states_list = [[877565,\"待办的\"],[877569,\"已确认\"],[1244182,\"技术评审中\"],[877573,\"修复中\"],[877572,\"验收中\"],[877567,\"已完成\"],[877574,\"已取消\"],[877568,\"已拒绝\"],[1244183,\"挂起\"]]\n",
    "states_map = {state[1]: state[0] for state in states_list}\n",
    "states_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9576a4a7-f2a2-4b23-9a13-9bbd7c2323cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def modify_issue_state(issueNo, state):\n",
    "    param = {\"issue[issue_state_id]\": state}\n",
    "    temp_headers = headers.copy()\n",
    "    temp_headers['Origin'] = 'https://gitee.com'\n",
    "    temp_headers['Host'] = 'gitee.com'\n",
    "    temp_headers['Refer'] = 'https://gitee.com/openharmony/kernel_linux_5.10/issues/'+issueNo[1:]\n",
    "    temp_headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0'\n",
    "    print('https://gitee.com/openharmony/kernel_linux_5.10/issues/'+ issueNo[1:] +'.json')\n",
    "    return requests.put('https://gitee.com/openharmony/kernel_linux_5.10/issues/'+ issueNo[1:] +'.json' ,headers = temp_headers, proxies=proxies, data=parse.urlencode(param))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "318b94c8-25f0-4f15-9b83-50455f35703a",
   "metadata": {},
   "outputs": [],
   "source": [
    "modify_issue_state( '#IAM0BZ', 877573).text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b767213-c82b-4bb8-9378-a8fd083d3c18",
   "metadata": {},
   "outputs": [],
   "source": [
    "temp_headers = headers.copy()\n",
    "temp_headers['Refer'] = 'Referer: https://gitee.com/openharmony/kernel_linux_5.10/issues/'+'#IAM0BZ'[1:]\n",
    "temp_headers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33810079-eea8-4875-8dbd-f8b9c5eacc15",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = requests.Session() \n",
    "resp = s.get('https://gitee.com/openharmony/kernel_linux_5.10/issues/IAM03K?from=project-issue', headers=headers, proxies=proxies)\n",
    "param = {\"issue[issue_state_id]\": 877573}\n",
    "print(resp.headers.get(\"Set-Cookie\"))\n",
    "print(\"----------------------\")\n",
    "print(s.cookies.get_dict())\n",
    "print(parse.urlencode(param))\n",
    "\n",
    "resp = s.put('https://gitee.com/openharmony/kernel_linux_5.10/issues/'+ 'IAM0BZ' +'.json' , proxies=proxies, data=parse.urlencode(param))\n",
    "print(s.cookies.get_dict())\n",
    "resp = s.get('https://gitee.com/openharmony/kernel_linux_5.10/issues/IAM03K?from=project-issue', headers=headers, proxies=proxies)\n",
    "print(resp.headers.get(\"Set-Cookie\"))\n",
    "print(s.cookies.get_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0204facd-e91b-4281-9bce-66b6746acd8b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
